package ysu.sxm.ptjc.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.SignMapper;
import ysu.sxm.ptjc.Service.SignService;
import ysu.sxm.ptjc.pojo.SignInfo;
import ysu.sxm.ptjc.pojo.UserInfo;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalTime;

@Service
public class SignServiceImpl extends ServiceImpl<SignMapper,SignInfo> implements SignService {
    @Resource
    SignMapper signMapper;

    @Override
    public SignInfo Login(Integer now) {
        //long time=16115379;
        //time=time*100000;
        //SignInfo signInfo=signMapper.findOneByUserIdAndSignBeginTime(now,new Timestamp(time));
        SignInfo signInfo=signMapper.findOneByUserIdAndSignBeginTime(now,new Timestamp(System.currentTimeMillis()));
        if(signInfo==null){
            SignInfo signInfo1 = new SignInfo();
            //signInfo1.setSignBeginTime(new Timestamp(time));
            signInfo1.setSignBeginTime(new Timestamp(System.currentTimeMillis()));
            signInfo1.setUser_id(now);
            if(signMapper.saveSign(signInfo1)){
                return signInfo1;}
            else{
                return null;
            }
        }else{
            signInfo.setUser_id(now);
            return signInfo;
        }
    }

    @Override
    public boolean Logout(SignInfo signInfo) {
        signInfo.setSignEndTime(new Timestamp(System.currentTimeMillis()));
        QueryWrapper<SignInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("sign_id",signInfo.getSignId()).eq("sign_begin_time",signInfo.getSignBeginTime());
        return this.update(signInfo,wrapper);
    }
}
